home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / ImageFilter.java < prev    next >
Text File  |  1998-09-22  |  6KB  |  170 lines

  1. /*
  2.  * @(#)ImageFilter.java    1.18 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.image;
  16.  
  17. import java.util.Hashtable;
  18.  
  19. /**
  20.  * This class implements a filter for the set of interface methods that
  21.  * are used to deliver data from an ImageProducer to an ImageConsumer.
  22.  * It is meant to be used in conjunction with a FilteredImageSource
  23.  * object to produce filtered versions of existing images.  It is a
  24.  * base class that provides the calls needed to implement a "Null filter"
  25.  * which has no effect on the data being passed through.  Filters should
  26.  * subclass this class and override the methods which deal with the
  27.  * data that needs to be filtered and modify it as necessary.
  28.  *
  29.  * @see FilteredImageSource
  30.  * @see ImageConsumer
  31.  *
  32.  * @version    1.18 07/01/98
  33.  * @author     Jim Graham
  34.  */
  35. public class ImageFilter implements ImageConsumer, Cloneable {
  36.     /**
  37.      * The consumer of the particular image data stream for which this
  38.      * instance of the ImageFilter is filtering data.  It is not
  39.      * initialized during the constructor, but rather during the
  40.      * getFilterInstance() method call when the FilteredImageSource
  41.      * is creating a unique instance of this object for a particular
  42.      * image data stream.
  43.      * @see #getFilterInstance
  44.      * @see ImageConsumer
  45.      */
  46.     protected ImageConsumer consumer;
  47.  
  48.     /**
  49.      * Returns a unique instance of an ImageFilter object which will
  50.      * actually perform the filtering for the specified ImageConsumer.
  51.      * The default implementation just clones this object.
  52.      */
  53.     public ImageFilter getFilterInstance(ImageConsumer ic) {
  54.     ImageFilter instance = (ImageFilter) clone();
  55.     instance.consumer = ic;
  56.     return instance;
  57.     }
  58.  
  59.     /**
  60.      * Filters the information provided in the setDimensions method
  61.      * of the ImageConsumer interface.
  62.      * @see ImageConsumer#setDimensions
  63.      */
  64.     public void setDimensions(int width, int height) {
  65.     consumer.setDimensions(width, height);
  66.     }
  67.  
  68.     /**
  69.      * Passes the properties from the source object along after adding a
  70.      * property indicating the stream of filters it has been run through.
  71.      */
  72.     public void setProperties(Hashtable props) {
  73.     props = (Hashtable) props.clone();
  74.     Object o = props.get("filters");
  75.     if (o == null) {
  76.         props.put("filters", toString());
  77.     } else if (o instanceof String) {
  78.         props.put("filters", ((String) o)+toString());
  79.     }
  80.     consumer.setProperties(props);
  81.     }
  82.  
  83.     /**
  84.      * Filter the information provided in the setColorModel method
  85.      * of the ImageConsumer interface.
  86.      * @see ImageConsumer#setColorModel
  87.      */
  88.     public void setColorModel(ColorModel model) {
  89.     consumer.setColorModel(model);
  90.     }
  91.  
  92.     /**
  93.      * Filters the information provided in the setHints method
  94.      * of the ImageConsumer interface.
  95.      * @see ImageConsumer#setHints
  96.      */
  97.     public void setHints(int hints) {
  98.     consumer.setHints(hints);
  99.     }
  100.  
  101.     /**
  102.      * Filters the information provided in the setPixels method of the
  103.      * ImageConsumer interface which takes an array of bytes.
  104.      * @see ImageConsumer#setPixels
  105.      */
  106.     public void setPixels(int x, int y, int w, int h,
  107.               ColorModel model, byte pixels[], int off,
  108.               int scansize) {
  109.     consumer.setPixels(x, y, w, h, model, pixels, off, scansize);
  110.     }
  111.  
  112.     /**
  113.      * Filters the information provided in the setPixels method of the
  114.      * ImageConsumer interface which takes an array of integers.
  115.      * @see ImageConsumer#setPixels
  116.      */
  117.     public void setPixels(int x, int y, int w, int h,
  118.               ColorModel model, int pixels[], int off,
  119.               int scansize) {
  120.     consumer.setPixels(x, y, w, h, model, pixels, off, scansize);
  121.     }
  122.  
  123.     /**
  124.      * Filters the information provided in the imageComplete method of
  125.      * the ImageConsumer interface.
  126.      * @see ImageConsumer#imageComplete
  127.      */
  128.     public void imageComplete(int status) {
  129.     consumer.imageComplete(status);
  130.     }
  131.  
  132.     /**
  133.      * Responds to a request for a TopDownLeftRight (TDLR) ordered resend
  134.      * of the pixel data from an ImageConsumer.
  135.      * The ImageFilter can respond to this request in one of three ways.
  136.      * <ol>
  137.      * <li>If the filter can determine that it will forward the pixels in
  138.      * TDLR order if its upstream producer object sends them
  139.      * in TDLR order, then the request is automatically forwarded by
  140.      * default to the indicated ImageProducer using this filter as the
  141.      * requesting ImageConsumer, so no override is necessary.
  142.      * <li>If the filter can resend the pixels in the right order on its
  143.      * own (presumably because the generated pixels have been saved in
  144.      * some sort of buffer), then it can override this method and
  145.      * simply resend the pixels in TDLR order as specified in the
  146.      * ImageProducer API.  <li>If the filter simply returns from this
  147.      * method then the request will be ignored and no resend will
  148.      * occur.  </ol> 
  149.      * @see ImageProducer#requestTopDownLeftRightResend
  150.      * @param ip The ImageProducer that is feeding this instance of
  151.      * the filter - also the ImageProducer that the request should be
  152.      * forwarded to if necessary.
  153.      */
  154.     public void resendTopDownLeftRight(ImageProducer ip) {
  155.     ip.requestTopDownLeftRightResend(this);
  156.     }
  157.     
  158.     /**
  159.      * Clones this object.
  160.      */
  161.     public Object clone() { 
  162.     try { 
  163.         return super.clone();
  164.     } catch (CloneNotSupportedException e) { 
  165.         // this shouldn't happen, since we are Cloneable
  166.         throw new InternalError();
  167.     }
  168.     }
  169. }
  170.